import axios from "axios";

const instance = axios.create({
  // baseURL: "http://kumanxuan1.f3322.net:8881/cms",
  baseURL: "/api",
  timeout: 5000,
});
//请求拦截器
instance.interceptors.request.use(
  (config) => {
    //在这里统一设置请求头  配置token
    //设置发送验证码请求的请求头，
    if (config.url === "/sendSMS" || config.url === "/wechatUsers/PCLogin") {
      config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    }
    let myToken = localStorage.getItem("x-auth-token");
    if (myToken) {
      //存在就携带上
      config.headers["x-auth-token"] = myToken;
    }

    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
//响应之前的拦截器
instance.interceptors.response.use(
  (res) => {
  
    //统一对响应数据进行一次处理(处理错误情况)
    if (res.data.code != 0 && res.data.code != 400 && res.data.code != 407) {
      alert(res.data.message);
      return false;
    }
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default instance;
